package top.yfq.shortlinkweb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import top.yfq.shortlinkweb.mapper.ShortMappingMapper;
import top.yfq.shortlinkweb.mapper.ShortUrlMapper;
import top.yfq.shortlinkweb.model.po.ShortMappingPO;
import top.yfq.shortlinkweb.model.po.ShortUrlPO;
import top.yfq.shortlinkweb.service.IShortUrlService;
import top.yfq.shortlinkweb.util.Pair;
import top.yfq.shortlinkweb.util.ShortCodeGenerator;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author yfq
 * @date 2025/7/18 18:11
 * @description
 */
@Service
public class ShortUrlService implements IShortUrlService {
    private final String baseUrl = "http://short.yaofengqiao.top/";

    @Resource
    private ShortUrlMapper shortUrlMapper;
    @Resource
    private ShortMappingMapper shortMappingMapper;
    @Resource
    private ShortCodeGenerator shortCodeGenerator;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getShortUrl(String url) {
        // 1. 解析参数
        Pair<String, Map<String, String>> pair = parseParams(url);
        String path = pair.key();
        Map<String, String> params = pair.value();

        // 2. 获取短链码
        String shortCode = shortCodeGenerator.getShortCode();

        // 3. 保存映射关系
        ShortUrlPO shortUrlPO = new ShortUrlPO();
        shortUrlPO.setShortCode(shortCode);
        shortUrlPO.setOriginUrl(path);
        shortUrlPO.setAccessCount(0);
        shortUrlPO.setExpireTime(LocalDateTime.now().plusDays(7));
        shortUrlPO.setCreateTime(LocalDateTime.now());
        shortUrlMapper.insert(shortUrlPO);

        List<ShortMappingPO> shortMappingPOS = params.entrySet().stream().map(entry -> {
            ShortMappingPO shortMappingPO = new ShortMappingPO();
            shortMappingPO.setShortCode(shortCode);
            shortMappingPO.setParamKey(entry.getKey());
            shortMappingPO.setParamValue(entry.getValue());

            return shortMappingPO;
        }).toList();
        for (ShortMappingPO shortMappingPO : shortMappingPOS) {
            shortMappingMapper.insert(shortMappingPO);
        }

        return baseUrl + shortCode;
    }

    @Override
    public String parseShortCode(String shortCode) {
        // 1. 查询缓存
        String url = null;

        // 2. 查询数据库
        ShortUrlPO shortUrlPO = shortUrlMapper.selectById(shortCode);
        List<ShortMappingPO> shortMappingPOS = queryByShortCode(shortCode);



        return null;
    }

    private List<ShortMappingPO> queryByShortCode(String shortCode) {
        return shortMappingMapper.selectList(Wrappers.<ShortMappingPO>lambdaQuery().eq(ShortMappingPO::getShortCode, shortCode));
    }

    public Pair<String, Map<String, String>> parseParams(String url) {
        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(url).build();
        String path = uriComponents.getScheme() + "://" + uriComponents.getHost();
        return new Pair<>(path, uriComponents.getQueryParams().toSingleValueMap());
    }
}
